package com.jdy.haoduoai.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jdy.haoduoai.model.PlaylistItem;
import com.ycsj.goldmedalnewconcept.R;

import java.util.List;

/**
 * 宝宝故事单
 * 
 * @author hf
 * 
 */
public class PlaylistAdapter extends BaseAdapter {

  private List<PlaylistItem> playlistItems;

  private final Context context;

  public PlaylistAdapter(Context c, List<PlaylistItem> data) {
    this.context = c;
    this.playlistItems = data;
  }

  public void setData(List<PlaylistItem> playlist) {

    this.playlistItems = playlist;
    notifyDataSetChanged();
  }

  // public void removeByPosition(int position) {
  // if (playlistItems != null && position >= 0 && position < getCount()) {
  // playlistItems.remove(position);
  // notifyDataSetChanged();
  // }
  // }

  @Override
  public int getCount() {

    return playlistItems.size();
  }

  @Override
  public PlaylistItem getItem(int position) {

    return playlistItems.get(position);
  }

  @Override
  public long getItemId(int position) {

    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    final PlaylistItem item = getItem(position);
    ViewHolder holder;
    if (convertView == null) {
      LayoutInflater li = (LayoutInflater) context
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = li.inflate(R.layout.listitem_playlist, parent, false);
      holder = new ViewHolder();
      holder.subtitleTv = (TextView) convertView
          .findViewById(R.id.episodeNameTextView);
      holder.orderTv = (TextView) convertView.findViewById(R.id.orderTextView);
      holder.timeRangeTv = (TextView) convertView
          .findViewById(R.id.rangeTimeTextView);
      holder.collectionNameTv = (TextView) convertView
          .findViewById(R.id.collectionNameTextView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    // ((SwipeListView) parent).recycle(convertView, position);

    holder.subtitleTv.setText(item.CollectionTitle);
    holder.orderTv.setText("第" + item.Order + "集");
    holder.timeRangeTv.setText("" + item.TimeRange);
    holder.collectionNameTv.setText(item.SubTitle);

    return convertView;
  }

  private class ViewHolder {
    TextView subtitleTv;
    TextView orderTv;
    TextView timeRangeTv;
    TextView collectionNameTv;
  }
}